package com.liliang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liliang.entity.Material;
import com.liliang.entity.MaterialPage;
import com.liliang.mapper.MaterialMapper;
import com.liliang.service.IMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liliang
 * @since 2021-11-04
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {


    @Override
    public MaterialPage findMaterialPage(Integer page, Integer max,Material material) {
        //创建分页对象
        MaterialPage materialPage=new MaterialPage();

        //创建查询工具
        QueryWrapper<Material> materialQueryWrapper = new QueryWrapper<Material>();

        //模糊查询
        if (material.getMaterialName()!=null){
            materialQueryWrapper.like("MATERIAL_NAME",material.getMaterialName());
        }
        if (material.getMaterialType()!=null){
            materialQueryWrapper.like("MATERIAL_TYPE",material.getMaterialType());
        }
        if (material.getMaterialNum()!=null){
            materialQueryWrapper.like("MATERIAL_NUM",material.getMaterialNum());
        }

        //模糊查询的总行数
        materialPage.setMax(this.count(materialQueryWrapper));

        //最后加入分页语句
        materialQueryWrapper.last("limit "+page+","+max);

        //分页查询
        materialPage.setMaterial(this.list(materialQueryWrapper));

        return materialPage;
    }

    @Override
    public List<Material> next(String[] nums) {
        List<Material> materials=new ArrayList<Material>();
        for (String num : nums) {
            materials.add(this.getById(num));
        }
        return materials;
    }
}
